﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using GUI.Custom_Classes;
using System.Windows.Controls;

namespace GUI.Tabs
{
    class DragAndDropController
    {
        private FrameworkElement dragging = null;
        private int position = -1;
        private ScriptViewer Script_Viewer;

        public DragAndDropController(ScriptViewer sv)
        {
            Script_Viewer = sv;
        }

        protected int indexOfSVClick(MouseEventArgs e)
        {
            if (Script_Viewer.Items.Count <= 0)
                return -1;

            // if clicking scroll bar return -1
            Point p = e.GetPosition(Script_Viewer);
            if (p.X > (Script_Viewer.ActualWidth - 20))
                return -1;
            if (p.Y > (Script_Viewer.ActualHeight - 20))
                return -1;
            /////////////////////////////////
            int i;
            for (i = 0; i < Script_Viewer.Items.Count - 1; i++)
            {
                FrameworkElement current = (Script_Viewer.Items.GetItemAt(i) as FrameworkElement);
                FrameworkElement next = (Script_Viewer.Items.GetItemAt(i + 1) as FrameworkElement);
                if (current != null)
                {
                    if (e.GetPosition(current).Y >= 0 && e.GetPosition(next).Y < 0)
                        return i;
                }
            }

            FrameworkElement last = (Script_Viewer.Items.GetItemAt(Script_Viewer.Items.Count - 1) as FrameworkElement);
            p = e.GetPosition(last);
            if (p.Y >= 0 && p.Y < 23)
            {
                return Script_Viewer.Items.Count - 1;
            }
            return -1;
        }

        public void ScriptViewerMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (dragging != null)
            {
                int index = indexOfSVClick(e);
                if (index != -1 && index != position)
                {
                    DropObjects(dragging, index);
                    RenameObjects(index);
                    dragging = null;
                }
            }
        }

        public void ScriptViewerMouseDown(object sender, MouseButtonEventArgs e)
        {
            dragging = null;
            int index = indexOfSVClick(e);
            if (index != -1)
            {
                dragging = (Script_Viewer.Items.GetItemAt(index) as FrameworkElement);
                position = index;
            }
        }

        private void DropObjects(Object source, int index)
        {
            Script_Viewer.Items.Remove(source);
            Script_Viewer.Items.Insert(index, source);
        }

        private void RenameObjects(int target)
        {
            int i = -1;
            int end = -1;
            if (position < target)
            {
                i = position;
                end = target;
            }
            else
            {
                i = target;
                end = position;
            }

            while (i <= end)
            {
                if (Script_Viewer.Items.GetItemAt(i) is TextBox)
                    ((TextBox)Script_Viewer.Items.GetItemAt(i)).Name = "ScriptViewerText" + i;
                else
                    ((ScriptBuilder)Script_Viewer.Items.GetItemAt(i)).Name = "ScriptViewerText" + i;
                i++;
            }
        }
    }
}
